/*
 * Created by JFormDesigner on Sun Jun 06 22:54:44 CST 2021
 */

package com.J1ay.carsys.gui.User;

import java.awt.event.*;

import com.J1ay.carsys.gui.Emp.Main;
import com.J1ay.carsys.gui.User.Dialog.BowCarDialog;
import com.J1ay.carsys.gui.Welcome;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.UserServiceImpl;
import com.J1ay.carsys.service.UserService;

import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class BowCar extends JFrame {
    UserService userService = new UserServiceImpl();
    private Car[] cars;
    Bower nowbower;
    String Phone;
    public BowCar(Bower bower) {
        initComponents();
        Phone = bower.getPhone();
        nowbower = bower;
    }

    public void getUnBowCar(){
        cars = (Car[]) userService.getUnBowCar().getData();
        String[] data = new String[cars.length];
        for (int i = 0 ;i < cars.length;++i){
            data[i] = cars[i].toString();
        }
        this.carlist.setListData(data);
    }

    public void start() {
        this.setVisible(true);
        this.getUnBowCar();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void mainActionPerformed(ActionEvent e) {
        // TODO add your code here
        new UserMain(nowbower).start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }

    private void exitActionPerformed(ActionEvent e) {
        // TODO add your code here
        new Welcome().start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }

    private void bowActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index = this.carlist.getSelectedIndex();
        if (index == -1) {
            new Warning(this,"您还未选中想要借的车辆！").start();
        }
        else {
            Car nowcar = cars[index];
            new BowCarDialog(this,nowcar,nowbower).start();
            System.out.println(nowcar);
            this.getUnBowCar();
        }

    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        carlist = new JList();
        label1 = new JLabel();
        bow = new JButton();
        panel1 = new JPanel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label5 = new JLabel();
        main = new JButton();
        exit = new JButton();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7ba1\u7406\u7cfb\u7edf");
        setResizable(false);
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(carlist);
        }

        //---- label1 ----
        label1.setText("\u6240\u5904\u4f4d\u7f6e\uff1a\u501f\u8f66");

        //---- bow ----
        bow.setText("\u501f\u8f66");
        bow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bowActionPerformed(e);
            }
        });

        //======== panel1 ========
        {

            //---- label2 ----
            label2.setText("\u7f16\u53f7");

            //---- label3 ----
            label3.setText("\u540d\u79f0");

            //---- label4 ----
            label4.setText("\u8f66\u724c\u53f7");

            //---- label6 ----
            label6.setText("\u8f66\u9f84");

            //---- label7 ----
            label7.setText("\u4ef7\u683c\\\u5929");

            //---- label5 ----
            label5.setText("\u7c7b\u578b");

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(label6, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(label7, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(62, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label5, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label4, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label3, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)))
            );
        }

        //---- main ----
        main.setText("\u8fd4\u56de");
        main.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mainActionPerformed(e);
            }
        });

        //---- exit ----
        exit.setText("\u9000\u51fa");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exitActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(scrollPane1)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(bow, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(main, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 309, Short.MAX_VALUE)
                            .addComponent(exit)
                            .addGap(15, 15, 15))
                        .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                        .addComponent(main, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                        .addComponent(exit, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
                    .addGap(1, 1, 1)
                    .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(65, 65, 65)
                            .addComponent(bow, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(19, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JList carlist;
    private JLabel label1;
    private JButton bow;
    private JPanel panel1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label6;
    private JLabel label7;
    private JLabel label5;
    private JButton main;
    private JButton exit;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
